package com.iscas.tech.controller;

import com.iscas.tech.entity.AtBasemodules;
import com.iscas.tech.param.AtBasemodulesPageParam;
import com.iscas.tech.service.AtBasemodulesService;
import com.iscas.tech.vo.AtBasemodulesQueryVo;
import com.iscas.tech.vo.MdAndMvQueryVo;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.util.LoginUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 项目基础信息模块表 控制器
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@RestController
@RequestMapping("/md")
@Api(value = "项目基础信息模块表API", tags = {"项目基础信息模块表"})
public class AtBasemodulesController extends BaseController {

    @Autowired
    private AtBasemodulesService atBasemodulesService;

    /**
     * 添加项目基础信息模块表
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加项目基础信息模块表", response = ApiResult.class)
    public ApiResult<Boolean> addAtBasemodules(AtBasemodules atBasemodules) throws Exception {
        if (LoginUtil.getUserId() == null) {
            return ApiResult.fail(ApiCode.UNAUTHORIZED);
        }
        atBasemodules.setCreatetitle(LoginUtil.getUserId().intValue());
        boolean flag = atBasemodulesService.saveAtBasemodules(atBasemodules);
        return ApiResult.result(flag);
    }

    /**
     * 修改项目基础信息模块表
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改项目基础信息模块表", response = ApiResult.class)
    public ApiResult<Boolean> updateAtBasemodules(AtBasemodules atBasemodules) throws Exception {
        boolean flag = atBasemodulesService.updateAtBasemodules(atBasemodules);
        return ApiResult.result(flag);
    }

    /**
     * 删除项目基础信息模块表
     */
    @RequestMapping(value = "delete", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除项目基础信息模块表", response = ApiResult.class)
    public ApiResult<Boolean> deleteAtBasemodules(String ids) throws Exception {
        String idArr[] = ids.split(",");
        for (String id : idArr) {
            atBasemodulesService.deleteAtBasemodules(Long.parseLong(id));
        }

        return ApiResult.ok();
    }

    /**
     * 获取项目基础信息模块表详情
     */
    @RequestMapping(value = "info", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "项目基础信息模块表详情", response = AtBasemodules.class)
    public ApiResult<AtBasemodulesQueryVo> getBaseModule(@NotNull(message = "id不能为空") Long id) throws Exception {
        return ApiResult.ok(atBasemodulesService.get(id));
    }

    /**
     * 项目基础信息模块表分页列表
     */
    @PostMapping("/getPageList")
    @ApiOperation(value = "项目基础信息模块表分页列表", response = AtBasemodules.class)
    public ApiResult<Paging<AtBasemodulesQueryVo>> getAtBasemodulesPageList(AtBasemodulesPageParam atBasemodulesPageParam) throws Exception {
        Paging<AtBasemodulesQueryVo> paging = atBasemodulesService.getAtBasemodulesPageList(atBasemodulesPageParam);
        return ApiResult.ok(paging);
    }

    /**
     * @param mdType {@link com.iscas.tech.bo.MdType} 所属功能
     * @return 获取全部基础模块信息
     * @throws Exception 查询异常
     */
    @GetMapping("/getAll")
    @ApiModelProperty(value = "获取全部基础模块信息")
    public ApiResult<List<AtBasemodulesQueryVo>> getAll(Integer mdType) throws Exception {
        return ApiResult.ok(atBasemodulesService.getAll(mdType));
    }

    /**
     * @param mdType {@link com.iscas.tech.bo.MdType}
     * @return 获取全部基础模块详细信息（含模块值列表）
     * @throws Exception 查询异常
     */
    @GetMapping("/getDetailAll")
    @ApiModelProperty(value = "获取全部基础模块详细信息（含模块值列表）")
    public ApiResult<List<MdAndMvQueryVo>> getDetailAll(Integer mdType) throws Exception {
        return ApiResult.ok(atBasemodulesService.getDetailAll(mdType));
    }
}

